<?php

namespace zhtfrontend\controllers;

use common\components\actions\DeleteAction;
use common\components\extenders\AjaxObject;
use common\components\helpers\Fun;
use common\components\helpers\Tree;
use doumiaoduo\buildform\BuildForm;
use zhtfrontend\models\datas\Admin;
use zhtfrontend\models\datas\FullActivities;
use zhtfrontend\models\datas\Mechanic;
use yii\data\Pagination;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
use yii\web\BadRequestHttpException;
use yii\web\NotFoundHttpException;
use yii\web\Response;
use yii\widgets\ActiveForm;

class FullactivitiesController extends \zhtfrontend\components\bases\BaseController
{
    public function actionIndex()
    {
        $param['searchData'] = \Yii::$app->request->get();
        $pageSize = isset($param['pageSize']) ? $param['pageSize'] : 12;
        $data = FullActivities::find()->orderBy('id ASC');
        if (!empty($param['searchData'])) {
            $search_data = array_filter($param['searchData']);
            if (!empty($search_data['status'])) {
                if ($search_data['status'] == 2) {
                    $search_data['status'] = 0;
                }
                $data->andWhere(['status' => $search_data['status']]);
            }
            if (!empty($search_data['type'])) {
                $data->andWhere(['type' => $search_data['type']]);
            }
        }
        $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $pageSize]);
        isset($param['page']) ? $pages->page = $param['page'] - 1 : 0;
        $model = $data->offset($pages->offset)->limit($pages->limit)->all();
        $array = Fun::asArray($model, ['operate']);
        return $this->render('index',['data'=>$array,'pages'=>$pages]);
    }


    public function actionCreate()
    {
        $model = new FullActivities();
        //ajax验证
        if (\Yii::$app->request->isAjax && \Yii::$app->request->post('ajax') == 'w0' && $model->load(\Yii::$app->request->post())) {
            \Yii::$app->response->format = Response::FORMAT_JSON;
            return ActiveForm::validate($model);
        }
        if ($model->load(\Yii::$app->request->post()) && $model->validate()) {
            $model->start_time = strtotime($model->start_time);
            $model->end_time = strtotime($model->end_time);
            if ($model->save(false)) {
                return $this->redirectMessage(\Yii::t('zhtfrontend', '添加成功'), Fun::url(['fullactivities/index']));
            }
        }
        return $this->render('create', ['model' => $model]);
    }

    public function actionUpdate()
    {
        $id = \Yii::$app->request->get('id');
        $model = FullActivities::findOne($id);
        $model->start_time = date('Y-m-d H:i', $model->start_time);
        $model->end_time = date('Y-m-d H:i', $model->end_time);
        //ajax验证
        if (\Yii::$app->request->isAjax && \Yii::$app->request->post('ajax') == 'w0' && $model->load(\Yii::$app->request->post())) {
            \Yii::$app->response->format = Response::FORMAT_JSON;
            return ActiveForm::validate($model);
        }
        if ($model->load(\Yii::$app->request->post()) && $model->validate()) {
            $model->start_time = strtotime($model->start_time);
            $model->end_time = strtotime($model->end_time);
            if ($model->save(false)) {
                return $this->redirectMessage(\Yii::t('zhtfrontend', '修改成功'), Fun::url(['fullactivities/index']));
            }
        }
        return $this->render('create', ['model' => $model]);
    }

}
